//
//  THChatEmitterManager.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//
#import "THChatEmitterManager.h"
#import "THShootAnimateView.h"

@interface THChatEmitterManager()

@end

@implementation THChatEmitterManager

+ (THChatEmitterManager *)sharedManager
{
    static THChatEmitterManager *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[THChatEmitterManager alloc]init];
    });
    return singleton;
}

+ (void)zh_snowImage:(NSArray<UIImage *> *)imageArray showInView:(UIView *)view
{
    [self zh_snowImage:imageArray showInView:view inset:UIEdgeInsetsZero duration:2];
}

+ (void)zh_snowImage:(NSArray<UIImage *> *)imageArray showInView:(UIView *)view duration:(CGFloat)duration
{
    [self zh_snowImage:imageArray showInView:view inset:UIEdgeInsetsZero duration:duration];
}

+ (void)zh_snowImage:(NSArray<UIImage *> *)imageArray showInView:(UIView *)view inset:(UIEdgeInsets)inset duration:(CGFloat)duration;
{
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    //创建一个CAEmitterLayer
    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer];

    //降落区域的方位
    snowEmitter.frame = CGRectMake(inset.left, inset.top, view.bounds.size.width - inset.left - inset.right, view.bounds.size.height - inset.top - inset.bottom);
    //添加到父视图Layer上
    [view.layer addSublayer:snowEmitter];

    //指定发射源的位置
    snowEmitter.emitterPosition = CGPointMake(view.layer.bounds.size.width / 2.0, -20);
    //指定发射源的大小
    snowEmitter.emitterSize  = CGSizeMake(view.layer.bounds.size.width, 20.0);
    //指定发射源的形状和模式
    snowEmitter.emitterShape = kCAEmitterLayerLine;
    snowEmitter.emitterMode  = kCAEmitterLayerSurface;
    //每秒多少个
    snowEmitter.birthRate = 4.0;
    snowEmitter.renderMode = kCAEmitterLayerBackToFront;
    CGFloat itemLeftTime = 6.0f;
    NSMutableArray <CAEmitterCell *> *cellArray = @[].mutableCopy;
    for (UIImage* image in imageArray) {

        //创建CAEmitterCell
        CAEmitterCell *snowflake = [CAEmitterCell emitterCell];

        snowflake.beginTime = CACurrentMediaTime()-1;
        //存活时间
        snowflake.lifetime = itemLeftTime;
        snowflake.lifetimeRange = itemLeftTime/2;
        //初速度，因为动画属于落体效果，所以我们只需要设置它在y方向上的加速度就行了。
        snowflake.velocity = 10;
        //初速度范围
        snowflake.velocityRange = 10;
        //y轴方向的加速度
        snowflake.yAcceleration = 200;
        
        snowflake.emissionLongitude = M_PI_2;
        //以锥形分布开的发射角度。角度用弧度制。粒子均匀分布在这个锥形范围内。
        snowflake.emissionRange = M_PI_4;
        //设置降落的图片
        snowflake.contents  = (id) [image CGImage];
        //图片缩放比例
        snowflake.scale = 0.5;
        //每秒多少个
        snowflake.birthRate = 1.0;

        [cellArray addObject:snowflake];
    }
    //开始动画
    snowEmitter.emitterCells = cellArray.copy;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        snowEmitter.birthRate = 0;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(itemLeftTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [snowEmitter removeFromSuperlayer];
        });
    });
}



/// 从上面射出image ，做落体运动
/// @param image image image对象
/// @param view view 显示在哪个view上，传nil默认windows
+ (void)zh_topLineShootImage:(UIImage *)image inView:(UIView *)view
{
    [self zh_topLineShootImage:image inView:view inset:UIEdgeInsetsZero];
}

/// 从上面射出image ，做落体运动
/// @param image image image对象
/// @param view view 显示在哪个view上，传nil默认windows
/// @param inset inset 边距，忽略上左右，只有下边距有效
+ (void)zh_topLineShootImage:(UIImage *)image inView:(UIView *)view inset:(UIEdgeInsets)inset
{
    if (!image) {
        return;
    }
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    THShootAnimateView *animateV = [[THShootAnimateView alloc] init];
    [view addSubview:animateV];
    [animateV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@(0));
        make.bottom.equalTo(@(-inset.bottom));
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [animateV topLineFire:image];
    });
}


#pragma mark - 左右射出动画

/// 从左右射出image ，做落体运动
/// @param image image image旋转
/// @param view view 显示在哪个view上，传nil默认windows
+ (void)zh_leftRightShootImage:(UIImage *)image inView:(UIView *)view
{
    [self zh_leftRightShootImage:image inView:view inset:UIEdgeInsetsZero];
}

/// 从左右射出image ，做落体运动
/// @param image image image旋转
/// @param view view 显示在哪个view上，传nil默认windows
+ (void)zh_leftRightShootImage:(UIImage *)image inView:(UIView *)view inset:(UIEdgeInsets)inset
{
    if (!image) {
        return;
    }
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    THShootAnimateView *animateV = [[THShootAnimateView alloc] init];
    [view addSubview:animateV];
    [animateV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@(0));
        make.bottom.equalTo(@(-inset.bottom));
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [animateV leftRightFire:image];
    });
    
}

@end
